export default {
    array: {
        delete: {
            byIndex(arr = [], index = -1) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                if (check.isInt(index) == false) {
                    check.throwError('index应为整数')
                }
                return [...arr.slice(0, index), ...arr.slice(index + 1)]
            },
            byKeyValue(arr = [], key = '', value = undefined) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                return arr.filter(item => item[key] != value)
            },
            byValue(arr = [], value = undefined) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                return arr.filter(item => item != value)
            },
            clearUndefined(arr = []) {
                if (check.isArray(arr) == false) {
                    check.throwError('arr应为数组')
                }
                return arr.filter(item => item != undefined)
            }
        },
        async format(arr = [], func = async (e) => { return e }) {
            if (check.isArray(arr) == false) {
                check.throwError('arr应为数组')
            }
            for (let i = 0; i < arr.length; i++) {
                arr[i] = await func(arr[i])
            }
            return arr
        },
    },
    object: {
        async for(obj = {}, func = async (key, value) => { return value }) {
            for (let i = 0; i < Object.keys(obj).length; i++) {
                obj[Object.keys(obj)[i]] = await func(Object.keys(obj)[i], obj[Object.keys(obj)[i]])
            }
            return obj
        }
    }
}

const check = {
    isArray(e) {
        return Array.isArray(e)
    },
    isInt(e) {
        return (typeof (e) == 'number') && (parseInt(e) == e)
    },
    isNumber(e) {
        return (typeof (e) == 'number')
    },
    isString(e) {
        return typeof (e) == 'string'
    },
    throwError(reason) {
        throw new Error(reason)
    }
}